//
//  UserInfoViewController.m
//  Consume100
//
//  Created by eliot on 16/3/23.
//  Copyright © 2016年 aleven. All rights reserved.
//

#import "UserInfoViewController.h"
#import "NickNameTableViewController.h"
#import "UserInfoTableViewCell.h"
#import "UIImageView+WebCache.h"

#import "SecurityUtil.h"
#import "GTMBase64.h"

@interface UserInfoViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableViewController;

@property (nonatomic, strong) UIImage *headImage;

@property (nonatomic, strong) NSString *currentNickName;

@property (nonatomic, strong) UserModel *myUserModel;

@property (nonatomic, strong) NSDictionary *dataDic;

@end

@implementation UserInfoViewController

+(UserInfoViewController*)CreatUserInfoViewController{
    
    UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UserInfoViewController *userInfo=[story instantiateViewControllerWithIdentifier:@"UserInfoViewController"];
    return userInfo;
}


-(UIImage *)headImage {
    if (!_headImage) {
        _headImage = [UIImage imageNamed:@"011"];
    }
    return _headImage;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSData *getthemeAndAboutData = [[NSUserDefaults standardUserDefaults] objectForKey:@"userModel"];
    self.myUserModel = [NSKeyedUnarchiver unarchiveObjectWithData:getthemeAndAboutData];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 5;
    NSDictionary *dic = @{@"consumerId":[SecurityUtil encryptAESData:self.myUserModel.id]};
    NSString *baseUrl = [NSString stringWithFormat:@"%@getConsumerInfo.jhtml", MyBaseURL];
    [manager POST:baseUrl parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            self.dataDic  = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            
            [self.tableViewController reloadData];
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
            [MBProgressHUD showError:@"链接超时,请检查网络"];
        });
        
        
    }];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人信息";
    self.tableViewController.tableFooterView = [[UIView alloc] init];
    self.tableViewController.backgroundColor = [UIColor colorWithRed:232/255.0 green:233/255.0 blue:232/255.0 alpha:1];
    [self.tableViewController registerNib:[UINib nibWithNibName:@"UserInfoTableViewCell" bundle:nil] forCellReuseIdentifier:@"headCell"];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.navigationController.navigationBar.translucent = NO;
}

#pragma mark - tableView (tableViewDelegate/tableViewDataSource)


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    cell.textLabel.font = [UIFont systemFontOfSize:19];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:19];
    if (indexPath.row == 0) {
        UserInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"headCell" forIndexPath:indexPath];
        
        
        [cell.headIV sd_setImageWithURL:[NSURL URLWithString:self.dataDic[@"img"]]];
    }else if (indexPath.row == 1){
        cell.detailTextLabel.text = self.dataDic[@"nickName"];
        cell.textLabel.text = @"昵称";
        UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_arrows"]];
        iv.frame = CGRectMake(0, 0, 18, 15);
        iv.contentMode = UIViewContentModeScaleAspectFit;
        cell.accessoryView = iv;
    }else {
        cell.textLabel.text = @"联系电话";
        cell.detailTextLabel.text = self.dataDic[@"phone"];
    }
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {//点击头像
        UIAlertController *alter=[UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *chat=[UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
    
            
        }];
        UIAlertAction *qq=[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
            
            
        }];
        UIAlertAction *cancle=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"取消");
        }];
        [alter addAction:chat];
        [alter addAction:qq];
;
        [alter addAction:cancle];
        [self presentViewController:alter animated:YES completion:nil];
    }
    if (indexPath.row == 1) {//点击昵称
        NickNameTableViewController *vc=[NickNameTableViewController CreatNickNameTableViewController];
        [self creatNav:vc];
    }
    if (indexPath.row == 2) {//点击联系电话
        
//        PhoneNumTableViewController *vc=[PhoneNumTableViewController CreatPhoneNumTableViewController];
//        [self creatNav:vc];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 100;
    }else {
        return 44;
    }
    
}


//创建模态导航控制器
-(void)creatNav:(UIViewController *)view{
    
    UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:view];
    nav.navigationBar.barTintColor = [UIColor colorWithRed:254/255.0 green:154/255.0 blue:37/255.0 alpha:1];
    view.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_back"] style:UIBarButtonItemStylePlain target:self action:@selector(dismiss)];
    [self.navigationController pushViewController:view animated:YES];
}

- (void)dismiss {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
